package com.example.demo.exception;

import com.example.demo.result.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.BindException;
import java.util.List;

//自己编写的异常处理器

@ControllerAdvice
public class GlobalExceptionHandler {
    //@Override
    // public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
    //    ModelAndView modelAndView=new ModelAndView();
    //     e.printStackTrace();
    //     if(e instanceof GlobalException){
    //         GlobalException e1 = (GlobalException) e;
    //         String codeMsg = e1.getCodeMsg().getMsg();
    //         System.out.println(codeMsg);
    //         modelAndView.addObject("msg",codeMsg);
    //     }else if (e instanceof BindException){//json等参数异常错误
    //         modelAndView.addObject("msg",CodeMsg.BIND_ERROR.getMsg());
    //     }else{
    //         modelAndView.addObject("msg",CodeMsg.SERVER_ERROR.getMsg());
    //     }
    //     modelAndView.setViewName("error");
    //     return modelAndView;
    // }
    @ExceptionHandler(value = Exception.class)//拦截所有的异常
    public ModelAndView exceptionHandler(Exception e, Model model){
        ModelAndView modelAndView=new ModelAndView();
        e.printStackTrace();
             if(e instanceof GlobalException){
                 GlobalException e1 = (GlobalException) e;
                 String codeMsg = e1.getCodeMsg().getMsg();
                 System.out.println(codeMsg);
                 modelAndView.addObject("msg",codeMsg);
             }else if (e instanceof BindException){//json等参数异常错误
                 modelAndView.addObject("msg",CodeMsg.BIND_ERROR.getMsg());
             }else{
                 modelAndView.addObject("msg",CodeMsg.SERVER_ERROR.getMsg());
             }
             modelAndView.setViewName("error");
             return modelAndView;
    }
   //private ModelAndView modelAndView;
   //@Override
   // public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
   //     e.printStackTrace();
   //     if(e instanceof GlobalException){
   //         GlobalException e1 = (GlobalException) e;
   //         String codeMsg = e1.getMessage();
   //         System.out.println(codeMsg);
   //         modelAndView.addObject("msg",codeMsg);
   //     }else if (e instanceof BindException){//json等参数异常错误
   //         modelAndView.addObject("msg",CodeMsg.BIND_ERROR.getMsg());
   //     }else{
   //         modelAndView.addObject("msg",CodeMsg.SERVER_ERROR.getMsg());
   //     }
   //     modelAndView.setViewName("error");
   //     return modelAndView;
   // }
}
